#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <zlib.h>
#include "libprlog/elog.h"
#include "libprtar/handle.h"

#define FILE_PATH_LEN 	256

/**
 * EasyLogger demo
 */
void test_elog(void) {
    while(true) {
        /* test log output for all level */
        log_a("Hello EasyLogger!");
        log_e("Hello EasyLogger: Error Test!");
        log_w("Hello EasyLogger!");
        log_i("Hello EasyLogger!");
        log_d("Hello EasyLogger!");
        log_v("Hello EasyLogger!");
//        elog_raw("Hello EasyLogger!");
        sleep(5);
    }
}


/*
 *  compress demo
 */
int pear_compress_files(const char *dest_file, const char **src_files, const int num)
{
   TAR  	*ptar;
   FILE 	*file;
   gzFile	gzfd;
   ssize_t  read_bytes;
   char     buffer[4096] = { 0 };
   int      i;

   // dest_file: 1.tar.gz, tar_file: 1.tar
   //
   char tar_file[128];
   strncpy(tar_file, dest_file, strlen(dest_file) - 3);

   // add data to 1.tar
   //
   if (tar_open(&ptar, tar_file, NULL, O_WRONLY | O_CREAT, 0644, TAR_GNU) < 0) { return -1; }

   for (i = 0; i < num; i++) {
   		if (tar_append_file(ptar, src_files[i], NULL) < 0) {
   			close(tar_fd(ptar));
   			return -1;
   		}
   }

   close(tar_fd(ptar));

   // compress: 1.tar -> 1.tar.gz
   //
   file = fopen(tar_file, "rb");
   if (NULL == file) return -1;

   gzfd = gzopen(dest_file, "wb");
   if (NULL == gzfd) {
       fclose(file);
       remove(tar_file);
       return -1;
   }

   while ((read_bytes = fread(buffer, 1, 4096, file)) > 0) {
       gzwrite(gzfd, buffer, read_bytes);
   }

   gzclose(gzfd);
   fclose(file);
   remove(tar_file);
   return 0;
}


int main(void)
{
	const char *src_files[] = { "./append.c", "./handle.h", "./libtar.c" };

	pear_compress_files("1.tar.gz", src_files, 3);

	/* close printf buffer */
	//setbuf(stdout, NULL);
	/* initialize EasyLogger */
	elog_init();
	/* set EasyLogger log format */
	elog_set_fmt(ELOG_LVL_ASSERT, ELOG_FMT_ALL);
	elog_set_fmt(ELOG_LVL_ERROR, ELOG_FMT_LVL | ELOG_FMT_TAG | ELOG_FMT_TIME | \
		     ELOG_FMT_P_INFO | ELOG_FMT_T_INFO | ELOG_FMT_DIR | ELOG_FMT_FUNC | ELOG_FMT_LINE);
	elog_set_fmt(ELOG_LVL_WARN, ELOG_FMT_LVL | ELOG_FMT_TAG | ELOG_FMT_TIME);
	elog_set_fmt(ELOG_LVL_INFO, ELOG_FMT_LVL | ELOG_FMT_TAG | ELOG_FMT_TIME);
	elog_set_fmt(ELOG_LVL_DEBUG, ELOG_FMT_ALL & ~ELOG_FMT_FUNC);
	elog_set_fmt(ELOG_LVL_VERBOSE, ELOG_FMT_ALL & ~ELOG_FMT_FUNC);
	elog_async_enabled(true);

	#ifdef ELOG_COLOR_ENABLE
    		elog_set_text_color_enabled(true);
	#endif

	pr_log_init("mylog", 10, 3);
	/* start EasyLogger */
	elog_start();

	///* dynamic set enable or disable for output logs (true or false) */
	//elog_set_output_enabled(false);
	///* dynamic set output logs's level (from ELOG_LVL_ASSERT to ELOG_LVL_VERBOSE) */
	//elog_set_filter_lvl(ELOG_LVL_WARN);
	///* dynamic set output logs's filter for tag */
	//elog_set_filter_tag("main");
	///* dynamic set output logs's filter for keyword */
	//elog_set_filter_kw("Hello");

	/* test logger output */
    	test_elog();

	return 0;
}


